import requests


class EmpMangeApi(object):
    # 添加员工
    def add_emp(self, req_header, req_body):
        resp = requests.post(url="http://ihrm2-test.itheima.net/api/sys/user",
                             headers=req_header,
                             json=req_body)
        return resp
    # 查询员工
    def get_emp(self, emp_id, req_header):
        resp = requests.get(url="http://ihrm2-test.itheima.net/api/sys/user/" + emp_id,
                            headers=req_header)
        return resp
    # 修改员工
    def update_emp(self, emp_id, req_header, req_body):
        resp = requests.put(url="http://ihrm2-test.itheima.net/api/sys/user/"+ emp_id,
                            headers=req_header,
                            json=req_body)
        return resp
    # 删除员工
    def delect_emp(self, emp_id, req_header):
        resp = requests.delete(url="http://ihrm2-test.itheima.net/api/sys/user/" + emp_id,
                               headers=req_header)
        return resp
if __name__ == '__main__':
    # 测试添加员工
    emp=EmpMangeApi()
    # 准备请求头
    # 登录,获取令牌
    resp = requests.post(url="http://ihrm2-test.itheima.net/api/sys/login",
                         json={"mobile": "13800000002", "password": "123456"})
    token = resp.json().get("data")
    req_header={"Content-Type": "application/json","Authorization": token}
    # 准备请求体
    req_body={"username":"丫丫","mobile": "13803135566", "workNumber": "123456"}
    emp.add_emp(req_header,req_body)
    print("添加员工",resp.json())

    # 测试查询员工
    emp_id="1545376394236669952"
    emp.get_emp(emp_id,req_header)
    print("查询员工",resp.json())
#
        # 修改
    emp_mod_req_body={"username":"哈哈"}
    emp.update_emp(emp_id,req_header,emp_mod_req_body)
    print("修改员工", resp.json())
#
#     删除
    emp.delect_emp(emp_id,req_header)
    print("删除员工", resp.json())
#
